package com.fzu.ess.common.utils;/**
 * Created by ercha on 2016/1/8.
 */

import com.alibaba.fastjson.JSON;
import com.fzu.ess.common.Model.FilterRule;

import java.util.Map;

/**
 * @author ercha
 * @ClassName: SearchUtils
 * @Description:
 * @Date 2016/01/08 13:42
 * @History:// 历史修改记录
 * <author>  // 修改人1
 * <time>    // 修改时间
 * <desc>    // 描述修改内容
 */
public class SearchUtils {
    /**
     *
     * @param filterText JSON 字符串，保存查询条件
     * @param entityName 要查询的类名
     * @return
     */
    public static String getSearchStr(String filterText, String entityName){

        String res = "";
        if(filterText==null || filterText.length()<1){
            return res;
        }

        Map<String, FilterRule> filterMap = JSON.parseObject(filterText, Map.class);

        //遍历所有字段
        for(Map.Entry entry: filterMap.entrySet()){

            String prop = entry.getKey().toString();
            System.out.println("key: " + prop + "\nvalue: " +
                    entry.getValue().toString());

            //遍历每个 FilterRule
            FilterRule fr;
            fr = JSON.parseObject(entry.getValue().toString(), FilterRule.class);
            res += fr.addRule(entityName, prop);
        }

        System.out.println("res: " + res);
        if(res.length()>4){
            return res.substring(0, res.length()-4);
        } else{
            return res;
        }
    }

}
